//
//  PayResultViewController.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/18.
//

import UIKit

class PayResultViewController: BaseViewController {
    var titleLab: UILabel!
    var payResultLab: UILabel!
    var goBackBtn: UIButton!
    var imgView: UIImageView!
    var payStatus: Bool = true
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navBar.isHidden = true
     
        
        titleLab = UILabel.init(frame: CGRect.init())
        titleLab.backgroundColor = UIColor.clear
        titleLab.textAlignment = .left
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        titleLab.textColor = Theme.titleLabColor
        titleLab.text = MMLocalizedString("result_payment", comment: "支付结果")
        self.view.addSubview(titleLab)
        
        
        imgView = UIImageView.init(frame: CGRect.zero)
        imgView.contentMode = .scaleToFill
        self.view.addSubview(imgView)
        
        payResultLab = UILabel.init(frame: CGRect.init())
        payResultLab.backgroundColor = UIColor.clear
        payResultLab.textAlignment = .center
        payResultLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        payResultLab.textColor = Theme.titleLabColor
        self.view.addSubview(payResultLab)
        
        self.goBackBtn = UIButton.init(frame: CGRect.zero)
        self.goBackBtn.setTitle(MMLocalizedString("back", comment: "返回"), for: .normal)
        self.goBackBtn.setTitle(MMLocalizedString("back", comment: "返回"), for: .highlighted)
        self.goBackBtn.setTitleColor(Theme.whiteColor, for: .normal)
        self.goBackBtn.setTitleColor(Theme.whiteColor, for: .highlighted)
        self.goBackBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        self.goBackBtn.layer.masksToBounds = true
        self.goBackBtn.layer.cornerRadius = 8
        self.goBackBtn.setBackgroundImage(UIImage.image(withColor: Theme.mainColor), for: .normal)
        self.goBackBtn.setBackgroundImage(UIImage.image(withColor: Theme.mainColor), for: .highlighted)
        goBackBtn.addTarget(self, action: #selector(goBackBtnAction), for: .touchUpInside)
        self.view.addSubview(goBackBtn)
        
        if payStatus {
            self.payResultLab.text = MMLocalizedString("pay_success", comment: "支付成功")
            self.imgView.image = UIImage.init(named: "icon_pay_success")
            self.goBackBtn.setTitle(MMLocalizedString("check_order", comment: "查看订单"), for: .normal)
            self.goBackBtn.setTitle(MMLocalizedString("check_order", comment: "查看订单"), for: .highlighted)
        }else{
            self.payResultLab.text = MMLocalizedString("pay_failed", comment: "支付失败")
            imgView.image = UIImage.init(named: "icon_pay_failed")
            self.goBackBtn.setTitle(MMLocalizedString("back", comment: "返回"), for: .normal)
            self.goBackBtn.setTitle(MMLocalizedString("back", comment: "返回"), for: .highlighted)
        }
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        
        
        titleLab.snp.makeConstraints { make in
            make.top.equalTo(NavBarHight - 44)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(44)
        }
        
        imgView.snp.makeConstraints { make in
            make.size.equalTo(CGSize.init(width: 101, height: 100))
            make.centerX.equalTo(kScreenWidth/2)
            make.top.equalTo(NavBarHight + 100)
        }
        
        payResultLab.snp.makeConstraints { make in
            make.top.equalTo(imgView.snp.bottom).offset(20)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(16)
        }
        
        self.goBackBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
            make.top.equalTo(self.payResultLab.snp.bottom).offset(40)
        }
    }
    
    ///navi back
    override func backAction() {
        self.goBackBtnAction()
    }
    
    ///返回订单列表
    @objc func goBackBtnAction(){
        self.navigationController?.popViewController(animated: true)
//        let vcArr = self.navigationController?.viewControllers ?? []
//        let tempArr = vcArr.filter { itemVC in
//            return
////            return itemVC.isKind(of: OrderViewController.self)
//        }
//        if tempArr.count > 0 {
//            self.navigationController?.popToViewController(tempArr.first!, animated: true)
//        }else{
//            self.navigationController?.popViewController(animated: true)
//        }
    }
}
